<?php
namespace Admin\Controller;
use Think\Controller;
class RbacController extends Controller {
    /*** 角色列表视图 */ 
    public function role(){
        $roleList = M("role")->select();
        $this->assign("roleList",$rolelist);
        layout("base");
        $this->display();
    }
    /*** 角色表单处理 */ 
    public function roleHandle(){  
        $id = I("get.id");  
        if(M("role")->where(array("id"=>$id))->delete()){  
            $this->success("删除成功",U("Admin/Index/role"));  
        }else{  
            $this->error("删除失败");  
        }  
    }
    /*** 设置权限表单处理 */ 
    public function setAccess(){
        $rid = I('rid',0,"intval");
        $db = M('access');
        $db->where(array('role_id'=>$rid))->delete();  
        $data = array();  
        foreach ($_POST['access'] as $v ) {  
            $tmp = explode('_', $v);  
            $data[] = array(  
                'role_id' => $rid,  
                'node_id' => $tmp[0],  
                'level' => $tmp[1]  
            );  
        }  
        if ($db->addAll($data)) {  
            $this->success('修改成功',U('Admin/Index/role'));  
        }else{  
            $this->error('修改失败');  
        }  
    }
    /*** 添加角色视图 */  
    public function addRole(){  
        layout("base");  
        $this->display();  
    }  
    /** * 添加角色表单处理 */  
    public function addRoleHandle(){  
        $data = I("post.");  
        if(M("role")->data($data)->add()){  
            $this->success("添加成功",U("Admin/Index/role"));  
        }else{  
            $this->error("添加角色失败，请稍后再试");  
        }  
    }  
   
    /*** 添加结点视图*/  
    public function addNode(){  
        $this->pid = I("pid",0,"intval");  
        $this->level = I("level",1,"intval");  
        switch ($this->level){  
            case 1:  
                $this->type = "应用";  
                break;  
            case 2:  
                $this->type = "控制器";  
                break;  
            case 3:  
                $this->type = "方法";  
                break;  
        }  
        layout("base");  
        $this->display();  
    }  
   
    /*** 添加结点表单处理*/  
    public function addNodeHandle(){  
        $data = I("post.");  
        if(M("node")->data($data)->add()){  
            $this->success("添加成功",U("Admin/Index/node"));  
        }else{  
            $this->error("添加失败，请稍后再试");  
        }  
    }  
}